package com.wnxy.f_confirm;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class RabbitConfirm implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        //correlationData 消息数据
        System.out.println("correlationData = " + correlationData);
        // ack 交换机是否收到消息
        System.out.println("ack = " + ack);
        // cause 消息失败的原因
        System.out.println("cause = " + cause);

        if (ack) {
            System.out.println("消息成功到达交换机！");
        }else {
            System.out.println("消息发送到交换机失败，接下来做容错处理，比如：重发，保存到db");
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息内容 = " + new String(message.getBody()));
        System.out.println("响应编码 = " + replyCode);
        System.out.println("消息无法路由的原因 = " + replyText);
        System.out.println("exchange = " + exchange);
        System.out.println("routingKey = " + routingKey);
    }
}
